package com.karry.util;

import com.karry.pojo.Blog;
import com.karry.pojo.Comment;
import com.karry.service.CommentService;
import com.karry.service.impl.CommentServiceImpl;
import org.omg.CORBA.ARG_OUT;

import java.util.ArrayList;

public class CommentUtil {

    public static CommentService cs = new CommentServiceImpl();

    //获取某篇文章全部一级评论
    public static ArrayList<Comment> getBlogCommentList(int id){
        return cs.searchBlogFatherComment(id);
    }

    //获取某个一级评论的全部回复
    public static ArrayList<Comment> getCommentBackList(int id){
        return cs.searchBackComment(id);
    }

    //获取文章集合的评论量(每篇文章的评论数包括其一级评论和对应的二级评论)
    public static ArrayList<Integer> getBlogCommentSizeList(ArrayList<Blog> blogs){
        ArrayList<Integer> list = new ArrayList<>();

        for (Blog blog : blogs) {
            int count = 0;
            //获取以及评论
            ArrayList<Comment> comments = getBlogCommentList(blog.getId());
            //统计一级评论数
            count += comments.size();
            //根据一级评论获取二级评论数
            for (Comment comment : comments) {
                ArrayList<Comment> commentBackList = getCommentBackList(comment.getId());
                //统计二级评论
                count += commentBackList.size();
            }
            list.add(count);
        }
        return list;
    }

    //获取某篇文章各一级评论的二级评论数量集合
    public static ArrayList<Integer> getBackCommentSizeList(int id){
        ArrayList<Integer> list = new ArrayList<>();

        //获取一级评论
        ArrayList<Comment> blogCommentList = getBlogCommentList(id);
        for (Comment comment : blogCommentList) {
            //获取每一条一级评论的回复(二级评论)
            ArrayList<Comment> backCommentSizeList = getCommentBackList(comment.getId());
            list.add(backCommentSizeList.size());
        }
        return list;
    }

    //获取某篇文章评论数量
    public static int getBlogCommentSize(int id){
        int count = 0;
        //获取一级评论
        ArrayList<Comment> blogCommentList = getBlogCommentList(id);
        //统计一级评论
        count += blogCommentList.size();
        //根据一级评论获取二级评论
        for (Comment comment : blogCommentList) {
            ArrayList<Comment> blogCommentList1 = getCommentBackList(comment.getId());
            //统计二级评论
            count += blogCommentList1.size();
        }
        return count;
    }

    //评论
    public static Comment doComment(String bId, int uID, String text, String cId, String buId, String lText){

        int bID = Integer.parseInt(bId);
        int cID = Integer.parseInt(cId);
        int bUID = Integer.parseInt(buId);

        return cs.insertComment(bID, uID, text, cID, bUID, lText);
    }

    //获取通知
    public static ArrayList<Comment> searchNotify(int id){
        return cs.searchNotify(id);
    }

    //清除通知标记
    public static int updateNotify(int bId, int bUId){
        return cs.updateNotify(bId, bUId);
    }

    //某个一级评论的回复数直接获取回复集合的size

}
